package com.woniuxy.microcurrentserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcurrentserver.dao.CurrentOrderDao;
import com.woniuxy.microcurrentserver.entity.CurrentFundAll;
import com.woniuxy.microcurrentserver.dao.CurrentFundAllDao;
import com.woniuxy.microcurrentserver.entity.CurrentFundHistory;
import com.woniuxy.microcurrentserver.entity.CurrentFundInfo;
import com.woniuxy.microcurrentserver.entity.CurrentFundMsgType;
import com.woniuxy.microcurrentserver.service.CurrentFundAllService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (CurrentFundAll)表服务实现类
 *
 * @author makejava
 * @since 2021-04-08 11:29:25
 */
@Service("currentFundAllService")
public class CurrentFundAllServiceImpl implements CurrentFundAllService {
    @Resource
    private CurrentFundAllDao currentFundAllDao;
    @Resource
    private CurrentOrderDao currentOrderDao;
    @Resource
    private RestTemplate restTemplate;

    @Override
    public PageInfo<CurrentFundAll> selectAllByType(Integer page,String name) {
        PageHelper.startPage(page,10);
        List<CurrentFundAll> currentFundAlls = currentFundAllDao.selectAllByType(name);
        PageInfo<CurrentFundAll> pageInfo = new PageInfo(currentFundAlls);
        return pageInfo;
    }

    /**
     * class:
     * create by: 陈酒
     * description:
     * create time: 2021/4/17
     *
     * @param: List<CurrentFundAll>
    */
    @Override
    public boolean insertAll(List<CurrentFundAll> list) {

        int i = currentFundAllDao.insertAll(list);

        return i>0;
    }

    /**
     * class: 
     * create by: 陈酒
     * description:
     * create time: 2021/4/17
     *
     * @param:a
     */
    @Override
    public boolean insertInfo(CurrentFundInfo data, List<CurrentFundHistory> millionCopiesIncome, List<CurrentFundHistory> sevenDaysYearIncome) {
        int i = 0;
        int j = 0;
        int k = 0;


             i = currentFundAllDao.insertInfo(data);
             j =currentFundAllDao.insertMillionCopiesIncome(millionCopiesIncome);
             if (j>0){
                 k =currentFundAllDao.insertSevendaysyearincome(sevenDaysYearIncome);
             }


        return i>0&&j>0&&k>0;
    }

    /**
     * class: 
     * create by: 陈酒
     * description:更新每日万份收益及每人个人收益
     * create time: 2021/4/17
     *
     * @param:a
     */
    @Override
    public boolean updateAll() {
        DateTime dateTime = new DateTime();
        String date = dateTime.toString("yyyyMMdd");
        String endDate = dateTime.toString("yyyy-MM-dd");
        String startDate = DateTime.parse(endDate).plus(-1).toString("yyyy-MM-dd");
        if(currentFundAllDao.enterDate(date)>0) {
            List<String> codes = currentFundAllDao.selectAllCode();

            for (String code : codes
            ) {
                String url = "https://api.doctorxiong.club/v1/fund/detail?code="+code+"&startDate="+startDate+"&endDate="+endDate;
                CurrentFundMsgType res = restTemplate.getForObject(url, CurrentFundMsgType.class);
                CurrentFundInfo data = res.getData();

                //每日更新业务
                currentFundAllDao.updateAll(data);
                List<CurrentFundHistory> millionCopiesIncome = new ArrayList<>();
                List<CurrentFundHistory> sevenDaysYearIncome = new ArrayList<>();


                for (String[] item:
                        data.getMillionCopiesIncomeData()) {
                    CurrentFundHistory currentFundHistory = new CurrentFundHistory();
                    currentFundHistory.setCode(code);
                    currentFundHistory.setUpdate(item[0]);
                    currentFundHistory.setMillionCopiesIncome(item[1]);
                    millionCopiesIncome.add(currentFundHistory);
                }

                for (String[] item:
                        data.getSevenDaysYearIncomeData()) {
                    CurrentFundHistory currentFundHistory = new CurrentFundHistory();
                    currentFundHistory.setCode(code);
                    currentFundHistory.setUpdate(item[0]);
                    currentFundHistory.setSevenDaysYearIncome(item[1]);
                    sevenDaysYearIncome.add(currentFundHistory);
                }


                //新增每日七日年化利率及万份收益
                currentFundAllDao.insertNewMillion(millionCopiesIncome.get(0));
                currentFundAllDao.updateNewSeven(sevenDaysYearIncome.get(0));

                //个人账户收益计算
                currentOrderDao.updateAccount(data.getMillionCopiesIncome());
            }
        }



        return false;
    }

    @Override
    public boolean checkCode(String code) {
        Integer id = currentFundAllDao.checkCode(code);

        return id!=null;
    }


}
